package com.sinodata.bsm.center.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.ResHealthDao;
import com.sinodata.bsm.common.vo.HealthStatusStatHour;

@Repository
public class ResHealthDaoImpl extends BaseDao implements ResHealthDao {

    @Override
    public void deleteHealthTransRule(Long parentResId) {
        getSession().createQuery("delete from HealthTransRule where parentResId=" + parentResId).executeUpdate();

    }

    private static SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void updateResHealthHisEndTime(Long resId, Date endtime) {
        getSession().createSQLQuery("update  health_status_his t set t.end_time = to_date('" + ff.format(endtime) + "','yyyy-MM-dd hh24:mi:ss') where end_time is null and res_id=" + resId).executeUpdate();
    }

    @SuppressWarnings("unchecked")
    public List<HealthStatusStatHour> queryResHealthHis(Long resId, String startTime, String endTime, int type) {
        String tb = type == 1 ? "REDUCE_HEALTH_HOUR " : "REDUCE_HEALTH_DAY ";
        //String format = type==1 ? "yyyy-MM-dd hh24:mi:ss":"yyyy-MM-dd";
        StringBuffer bf = new StringBuffer("select * from ");
        bf.append(tb);
        bf.append(" where TIME between ");
        bf.append(" to_date('" + startTime + "','yyyy-MM-dd hh24:mi:ss') ");
        bf.append(" and to_date('" + endTime + "','yyyy-MM-dd hh24:mi:ss') ");
        bf.append(" and RES_ID= " + resId + " order by time desc");
        PreparedStatement pStmt = null;
        List<HealthStatusStatHour> results = new ArrayList<HealthStatusStatHour>();
        ResultSet rs = null;
        try {
            pStmt = getSession().connection().prepareStatement(bf.toString());
            rs = pStmt.executeQuery();
            while (rs.next()) {
                HealthStatusStatHour vo = new HealthStatusStatHour();
                vo.setTime(rs.getTimestamp("TIME"));
                vo.setNormalTime(rs.getBigDecimal("NORMAL_TIME").longValue());
                vo.setMinorTime(rs.getBigDecimal("MINOR_TIME").longValue());
                vo.setWarnTime(rs.getBigDecimal("WARN_TIME").longValue());
                vo.setMajorTime(rs.getBigDecimal("MAJOR_TIME").longValue());
                vo.setCriticalTime(rs.getBigDecimal("CRITICAL_TIME").longValue());
                vo.setDegree(rs.getInt("DEGREE"));
                results.add(vo);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
                pStmt.close();
            } catch (SQLException e) {
                rs = null;
                pStmt = null;
            }

        }
        return results;
    }

}
